package kz.kstu.scantest.facade.ejb.impl;

import kz.kstu.scantest.business.server.api.Service;
import kz.kstu.scantest.business.server.context.AbstractServerContextManager;
import kz.kstu.scantest.business.server.context.ServerContext;

import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @author Filipp_Stankevich
 *         created: 02.08.11 7:48
 */
public class AbstractFacadeBean extends AbstractServerContextManager{

    @PersistenceContext(unitName = "scantestPU")
    private EntityManager entityManager;
    @Resource
    private SessionContext sessionContext;

    @Override
    protected <T extends Service>T getService(Class<T> serviceInterface) {
        ServerContext serverContext = createServerContext();
        super.setServerContext(serverContext);
        return super.getService(serviceInterface);
    }

    private ServerContext createServerContext() {
        ServerContext serverContext = new ServerContext();
        serverContext.setEntityManager(entityManager);
        return serverContext;
    }

    public SessionContext getSessionContext() {
        return sessionContext;
    }
}
